package cn.hrlly.tao2shou.callback;

import com.google.gson.Gson;
import com.zhy.http.okhttp.callback.Callback;

import cn.hrlly.tao2shou.R;
import cn.hrlly.tao2shou.domain.RequireList;
import cn.hrlly.tao2shou.listener.OnMyRequireListListener;
import okhttp3.Request;
import okhttp3.Response;

/**
 * User: gengliang
 * Date: 2016-01-30
 * Time: 09:40
 */

public class MyRequireListCallback extends Callback<RequireList> {

    private OnMyRequireListListener onMyRequireListListener;

    public MyRequireListCallback(OnMyRequireListListener onMyRequireListListener){
        this.onMyRequireListListener=onMyRequireListListener;
    }

    @Override
    public RequireList parseNetworkResponse(Response response) throws Exception {
        String string=response.body().string();
        RequireList requireList=new Gson().fromJson(string,RequireList.class);
        return requireList;
    }

    @Override
    public void onError(Request request, Exception e) {
        onMyRequireListListener.showMyRequireListFail(R.string.network_connection_error);
    }

    @Override
    public void onResponse(RequireList requireList) {
        if (requireList.getStatus()==0)
            onMyRequireListListener.showMyRequireListSucc(requireList);
        else
            onMyRequireListListener.showMyRequireListFail(requireList.getErrorMessage());
    }
}
